package com.changgou.goods.service.impl;

import com.changgou.goods.dao.AlbumMapper;
import com.changgou.goods.pojo.Album;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author zhengjd
 * @date 2020/5/2 10:12
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    /***
     * Album多条件分页查询
     * @param album
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Album> findPage(Album album, int page, int size) {
        //设置分页条件
        PageHelper.startPage(page, size);
        Example example = createExample(album);
        List<Album> albums = albumMapper.selectByExample(example);
        return new PageInfo<>(albums);
    }

    /***
     * Album分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Album> findPage(int page, int size) {
        //设置分页条件
        PageHelper.startPage(page,size);

        return new PageInfo<>(albumMapper.selectAll());
    }

    /***
     * Album多条件搜索方法
     * @param album
     * @return
     */
    @Override
    public List<Album> findList(Album album) {
        //构建查询条件对象
        Example example = createExample(album);
        return albumMapper.selectByExample(example);
    }

    private Example createExample(Album album) {
        //创建查询条件
        Example example = new Example(Album.class);
        //创建条件构造器
        Example.Criteria criteria = example.createCriteria();
        if (album != null){
            if (StringUtils.isNotEmpty(album.getId()+"")) {
                //主键
                criteria.andEqualTo("id", album.getId());
            }
            if (StringUtils.isNotEmpty(album.getImage())) {
                //相册封面
                criteria.andEqualTo("image",  album.getImage() );
            }
            if (StringUtils.isNotEmpty(album.getTitle())) {
                //相册名称
                criteria.andLike("title", "%"+album.getTitle()+"%");
            }
            if (StringUtils.isNotEmpty(album.getImageItems())) {
                //图片列表
                criteria.andEqualTo("imageItems", album.getImageItems());
            }
        }

        return example;
    }

    /***
     * 删除Album
     * @param id
     */
    @Override
    public void delete(Long id) {
        albumMapper.deleteByPrimaryKey(id);
    }

    /***
     * 修改Album数据
     * @param album
     */
    @Override
    public void update(Album album) {
        albumMapper.updateByPrimaryKey(album);
    }

    /***
     * 新增Album
     * @param album
     */
    @Override
    public void add(Album album) {
        albumMapper.insert(album);
    }

    /**
     * 根据ID查询Album
     *
     * @param id
     * @return
     */
    @Override
    public Album findById(Long id) {
        return albumMapper.selectByPrimaryKey(id);
    }

    /***
     * 查询所有Album
     * @return
     */
    @Override
    public List<Album> findAll() {
        return albumMapper.selectAll();
    }
}
